package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.CurrentUserInfo;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.mapper.AgencyCertificationAuditMapper;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.service.AgencyCertficationAuditService;
import com.jzo2o.customer.service.IAgencyCertificationService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

@Service
public class AgencyCertficationAuditServiceImpl extends ServiceImpl<AgencyCertificationAuditMapper, AgencyCertificationAudit> implements AgencyCertficationAuditService {
    @Autowired
    private IAgencyCertificationService agencyCertificationService;
    @Autowired
    private AgencyCertificationAuditMapper agencyCertificationAuditMapper;
    /**
     * 机构端提交认证申请
     */
    @Override
    public void agencyCertificationAudit(AgencyCertificationAuditAddReqDTO dto) {
        // 机构认证审核表
        AgencyCertificationAudit agencyCertificationAudit = BeanUtil.copyProperties(dto, AgencyCertificationAudit.class);
        agencyCertificationAudit.setServeProviderId(UserContext.currentUserId());
        agencyCertificationAudit.setAuditStatus(0);
        agencyCertificationAudit.setCertificationStatus(1);
        agencyCertificationAudit.setCreateTime(new Date());
        agencyCertificationAudit.setUpdateTime(new Date());
        agencyCertificationAuditMapper.add(agencyCertificationAudit);
        // 机构认证信息表
        AgencyCertification agency = BeanUtil.copyProperties(dto, AgencyCertification.class);
        agency.setId(UserContext.currentUserId());
        agency.setCertificationStatus(1);
        agency.setCertificationTime(LocalDateTime.now());
        agencyCertificationService.save(agency);
    }

    /**
     * 查询最新的驳回原因
     */
    @Override
    public String agencyRejectReason() {
        Long userId = UserContext.currentUserId();
//        AgencyCertificationAudit one = lambdaQuery()
//                // 查询条件
//                .eq(AgencyCertificationAudit::getServeProviderId, userId)
//                // 根据创建时间倒序
//                .orderByDesc(AgencyCertificationAudit::getCreateTime)
//                // 截取前1条数据
//                .last("limit 1")
//                // 返回截取的第一条数据
//                .one();
        QueryWrapper<AgencyCertificationAudit> last = new QueryWrapper<AgencyCertificationAudit>()
                .eq("serve_provider_id", userId)
                .orderByDesc("create_time")
                .last("limit 1");
        AgencyCertificationAudit one = baseMapper.selectOne(last);
        return one.getRejectReason();
    }

    /**
     * 审核服务人员认证分页查询
     */
    @Override
    public PageResult<AgencyCertificationAudit> queryPageAgency(AgencyCertificationAuditPageQueryReqDTO dto) {
        //调用mapper查询数据，这里由于继承了ServiceImpl<ServeMapper, Serve>，使用baseMapper相当于使用ServeMapper
        return PageHelperUtils.selectPage(dto, () -> baseMapper.queryList(dto.getName(), dto.getLegalPersonName(), dto.getAuditStatus(), dto.getCertificationStatus()));
    }

    /**
     * 审核服务人员认证信息
     */
    @Override
    public void auditServicePersonnel(Integer certificationStatus, Long id, String rejectReason) {
        CurrentUserInfo currentUserInfo = UserContext.currentUser();
        // 服务人员认证审核表
        AgencyCertificationAudit agency = baseMapper.selectById(id);
        agency.setAuditStatus(1);
        agency.setAuditorId(currentUserInfo.getId());
        agency.setAuditorName(currentUserInfo.getName());
        agency.setAuditTime(new Date());
        agency.setCertificationStatus(certificationStatus);
        agency.setRejectReason(rejectReason);
        agency.setUpdateTime(new Date());
        baseMapper.updateById(agency);
        // 服务人员认证信息表
        AgencyCertification agencyCertification = agencyCertificationService.getById(agency.getServeProviderId());
        agencyCertification.setId(agency.getServeProviderId());
        agencyCertification.setCertificationStatus(certificationStatus);
        agencyCertificationService.updateById(agencyCertification);
    }
}
